﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Activities;

namespace Foulds.Calculator.StateMachine.Activities
{
    /// <summary>
    /// This activity concatenates to variables together and
    /// puts the results in the variable provided.
    /// </summary>
    public sealed class ConcatenationActivity : CodeActivity<string>
    {
        #region Arguments
        public InArgument<object> Value1 { get; set; }
        public InArgument<object> Value2 { get; set; }
        #endregion

        #region Execute
        /// <summary>
        /// Execute the code for the custom activity.
        /// </summary>
        /// <returns>Return the concatenated string value.</returns>
        protected override string Execute(CodeActivityContext context)
        {
            // get the values
            object value1 = context.GetValue(this.Value1);
            object value2 = context.GetValue(this.Value2);

            // get the value text
            string value1Text = this.GetValueString(value1);
            string value2Text = this.GetValueString(value2);

            //this.Result.Set(context, resultString);
            return string.Concat(value1Text, value2Text);
        }
        #endregion

        #region Get Value String
        /// <summary>
        /// Get the string value of the object provided.
        /// </summary>
        /// <param name="value">The value to process.</param>
        /// <returns>Return the string value of the object.</returns>
        private string GetValueString(object value)
        {
            string result;
            
            if (value == null)
            {
                result = string.Empty;
            }
            else
            {
            Type valueType = value.GetType();

            if (valueType == typeof(String))
                result = (string)value;
            else if (valueType == typeof(KeyName))
                result = Convert.ToInt32((KeyName)value).ToString(CultureInfo.InvariantCulture);
            else
                result = value.ToString();                
            }

            return result;
        } 
        #endregion
    }
}
